package org.itheima.service.impl;

import org.itheima.mapper.UserMapper;
import org.itheima.pojo.User;
import org.itheima.service.UserService;
import org.itheima.utils.Md5Util;
import org.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service//  @Service  将当前类注册 ico 容器里面
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;//需要使用userMapper的sql查询到的数据
    @Override
    //用户名的获取，注册账户
    public User findByUserName(String username) {
        User user = userMapper.findByUserName(username);
        return user;
    }

    //密码设置
    @Override
    public void register(String username, String password) {
        //密码加密,MD5加密算法，有一个getMD5的工具类，调用就可以
        String md5String = Md5Util.getMD5String(password);//将我们的原文密码password传进去得到密文密码md5String

        //调用mapper层添加数据
        userMapper.add(username,md5String);
    }

    //添加第一次注册的时间
    @Override
    public void update (User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    //更新头像
    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("Id");
        userMapper.avatarUrl(avatarUrl,id);//传数据给UserMapper接口
    }

    /*
     * userMapper：接口
     * avatarUrl：新的方法名，在userMapper里创建，书写sql语句
     * */

    //修改密码
    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("Id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }

}
